package com.ciwong.epaper.modules.epaper.util;

import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.ciwong.epaper.R;
import com.ciwong.epaper.modules.me.bean.CheckValidServiceBean;
import com.ciwong.epaper.util.EConstants;
import com.ciwong.libs.utils.CWResource;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.List;

/**
 * Created by lzr on 2016/9/20.
 */
public class CSerDialog extends Dialog {

    private Context mContext;
    private List<CheckValidServiceBean> mListObj;
    private LayoutInflater mInflater;
    private LinearLayout contentLayout;
    private CSIntface mCSClick;
    private HorizontalScrollClick mScrollView;
    private View mLeft,mRight;
//    private  TextView tv_msg;
    private TextView tv_title;


    /**
     * 点击事件的回调接口
     */
    public interface CSIntface{
         void ClickItem(View view,CheckValidServiceBean service);
    }

    public CSerDialog(Context context,List<CheckValidServiceBean> mListObj){
        super(context, CWResource.getStyles("libs_cw_dialog"));
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.scer_dialog);
        mInflater = LayoutInflater.from(getContext());
        this.mListObj=mListObj;
        this.mContext=context;
        contentLayout=(LinearLayout)findViewById(R.id.ima_lins);
        mScrollView=(HorizontalScrollClick)findViewById(R.id.scr_v);
        mScrollView.setScrollEvent(scorEve);
        mLeft=findViewById(R.id.t1);
        mRight=findViewById(R.id.t2);

//        tv_msg = (TextView) findViewById(R.id.tv_msg);
        tv_title = (TextView) findViewById(R.id.tv_title);

        init();
    }

    private HorizontalScrollClick.ScrollEvent scorEve=new HorizontalScrollClick.ScrollEvent(){
        @Override
        public void toLeft() {
            mLeft.setVisibility(View.GONE);
            mRight.setVisibility(View.VISIBLE);
        }

        @Override
        public void toRight() {
            mLeft.setVisibility(View.VISIBLE);
            mRight.setVisibility(View.GONE);
        }

        @Override
        public void other() {
            mLeft.setVisibility(View.VISIBLE);
            mRight.setVisibility(View.VISIBLE);
        }
    };
    public void setMsg (String msg){
//        tv_msg.setVisibility(View.VISIBLE);
//        tv_msg.setText(msg);
    }
    public void setTitle(String title){
        tv_title.setText(title);
    }
    public CSerDialog(Context context, List<CheckValidServiceBean> mListObj, CSIntface mCSClick){
        this(context,mListObj);
        this.mCSClick=mCSClick;
    }


    private void init(){
            for(int i=0;i<mListObj.size();i++){
                final CheckValidServiceBean tempService=mListObj.get(i);
                View view=mInflater.inflate(R.layout.cser_item,null);
//                View month=(ImageView)view.findViewById(R.id.month_id);
                ImageView mIocn=(ImageView)view.findViewById(R.id.item_icon);
                TextView mText=(TextView)view.findViewById(R.id.item_text);
                mText.setText(tempService.name);
//                if(tempService.getSign()==0){
//                    month.setVisibility(View.VISIBLE);
//                }else
                {
//                    month.setVisibility(View.GONE);
                }
                ImageLoader.getInstance().displayImage(tempService.logo, mIocn,
                        EConstants.getDefAvatarCircleBuilder());
                view.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if(mCSClick!=null){
                            dismiss();
                            mCSClick.ClickItem(v,tempService);
                        }
                    }
                });
                contentLayout.addView(view);
            }
            if(mListObj.size()==2){
                FrameLayout.LayoutParams params=(FrameLayout.LayoutParams)contentLayout.getLayoutParams();
                params.gravity=Gravity.CENTER_HORIZONTAL;
                contentLayout.setLayoutParams(params);
            }
        }

    }

